package com.yhm.net.BIO服务端对客户端连接交由单独的线程在线程池中执行用以支撑高连接量但是处理速度比较慢;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BIOClient {
  public static void main(String[] args) {
    int num = 9999;
    //使用JAVA高并发java.util.concurrent包下的 计数器 实现高并发
    CountDownLatch countDownLatch = new CountDownLatch(num);
    ExecutorService executorService = Executors.newFixedThreadPool(num);
    for(int i=0;i<num;i++  ) {
      executorService.execute(new Runnable() {
        @Override
        public void run() {
          try {

            Socket socket = new Socket("127.0.0.1", 8080);
            countDownLatch.await();
            try {
              Thread.sleep(10000);//大家都睡眠10秒
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            OutputStream outputStream = socket.getOutputStream();
            String str = Math.random() + "随机数据";
            outputStream.write(str.getBytes());

            outputStream.close();
            socket.close();
          } catch (IOException e) {
            e.printStackTrace();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      });
      countDownLatch.countDown();
    }
    executorService.shutdown();




//    try {
//      Thread.sleep(15000);//主线程
//    } catch (InterruptedException e) {
//      e.printStackTrace();
//    }
//    countDownLatch.countDown();
  }

  }
